import React from 'react';
import { Space, Table } from 'antd';
import axios from 'axios'
import  { useEffect, useState } from 'react';

const download = (article_id) => {
  axios.post('/api/downloadArticle',
    {
      "id": article_id,
    }, {responseType: "arraybuffer"}).then(
      res => {
        let blob = new Blob([res.data],{type: 'application/vnd.ms-excel'});
        // const url = window.URL.createObjectURL(new Blob([res.data]));
        const url = window.URL || window.webkitURL || window.moxURL;
        const link = document.createElement('a'); //创建a标签
        link.href = url.createObjectURL(blob);
        link.style.display = 'none';
        link.download = res.headers['content-disposition'].split(';')[1].split('=')[1];
        document.body.appendChild(link);
        link.click();
        URL.revokeObjectURL(link.href); // 释放 URL对象
        document.body.removeChild(link);
      }
    );
}

var dataSource

const request = axios.create({
  baseURL: "http://localhost:8080",
});

const pass = (index) => {
  var store = window.localStorage
  var user_name = store.getItem("UserName")
  var review_data = {
    "reviewer": user_name,
    "id": index.id,
    "review": true
  }
  request.post("/api/review", review_data).then (  
    res => {  // 成功的回调函数
      alert("审核成功")
      window.location.reload()
    }
  ).catch(res => {  // 错误处理
      console.log(res.data.res)
      console.log("user_fail")
  })
}

const reject = (index) => {
  var store = window.localStorage
  var user_name = store.getItem("UserName")
  var review_data = {
    "reviewer": user_name,
    "id": index.id,
    "review": false
  }
  request.post("/api/review", review_data).then (  
    res => {  // 成功的回调函数
      alert("审核成功")
      window.location.reload()
    }
  ).catch(res => {  // 错误处理
      console.log(res.data.res)
      console.log("user_fail")
  })
}

const columns1 = [
    {
      title: '名称',
      dataIndex: 'title',
      key: 'index',
    },
    {
      title: '上传用户',
      dataIndex: 'uploader',
      key: 'index',
    },
    {
        title: '下载',
        key: 'index',
        render: (index) => (
            <Space size="middle">
              <a onClick={() => download(index.id)}>下载</a>
            </Space>
        ),
    },
    {
        title: '审核',
        key: 'index',
        render: (index) => (
            <Space size="middle">
              <a onClick={() => pass(index)}>通过</a>
              <a onClick={() => reject(index)}>不通过</a>
            </Space>
        ),
    }
];

const AdminTable1 = () => {
  const [TableData, setTableData] = useState("")

  useEffect(()=>{
    const getData = async() => {
      var store = window.localStorage
      var class_name = store.getItem("ClassPage")
      var user_name = store.getItem("UserName")
      const request = axios.create({
        baseURL: "http://localhost:8080"
      });
      var table_data = {
        "course": class_name,
        "user": user_name
      }
      request.post('/api/getUnreviewArticle', table_data).then(  
        res => {  
          dataSource = res.data
          console.log(res.data)
          let tmp = [...dataSource]
          setTableData(tmp)
        }
      ).catch(res => {  // 错误处理
        console.log(res.data.res)
      })
    }
    getData()
  },[])

  return (
    <Table dataSource={TableData} columns={columns1} />
  )
}

export default AdminTable1